package game;


import java.io.Serializable;
import java.util.ArrayList;

public class Player implements Serializable{

	private static final long serialVersionUID = -6119089707691604436L;
	private String name;
	private double moneyLeft;
	private double currentBet;
	private ArrayList<Card> cards;
	
	public Player(String name, int initialMoney) {
		this.name = name;
		this.moneyLeft = initialMoney;
		this.currentBet = 0;
		this.cards = new ArrayList<Card>(2);
	}
	/**
	 * Deals one card to player.
	 * @param card Card to deal
	 */
	public void dealCard(Card card) {
		synchronized (cards) {
			this.cards.add(card);
		}
	}
	/**
	 * Remove all cards from player.
	 * Also current bet = 0.
	 */
	public void clearCards() {
		synchronized (cards) {
			cards.removeAll(cards);
			currentBet = 0;
		}
	}
	/**
	 * @return the cards
	 */
	public ArrayList<Card> getCards() {
		synchronized (cards) {
			return cards;
		}
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the moneyLeft
	 */
	public double getMoneyLeft() {
		return moneyLeft;
	}

	/**
	 * Add money to existing moneyLeft
	 * @param moneyLeft the moneyLeft to set
	 */
	public void addMoney(double money) {
		this.moneyLeft += money;
	}

	/**
	 * @return the currentBet
	 */
	public double getCurrentBet() {
		return currentBet;
	}

	/**
	 * Bet money. Adds money to the currently bet money.
	 * Also decreases moneyLeft.
	 * @param bet Money to bet
	 */
	public void bet(double bet) {
		this.currentBet += bet;
		this.moneyLeft -= bet;
	}
	/**
	 * Set player's money to zero.
	 */
	public void setMoneyZero() {
		this.moneyLeft = 0;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + currentBet);
		result = (int) (prime * result + moneyLeft);
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Player other = (Player) obj;
		if (currentBet != other.currentBet)
			return false;
		if (moneyLeft != other.moneyLeft)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Player [name=" + name + "]";
	}
	
	
}
